<template>
  <div>
    <form-create ref="fc" v-model="fApi" :option="options" :rule="rule" />
  </div>
</template>

<script>
export default {
  data() {
    return {
      formData: {},
      rule: [],
      fApi: {},
      options: {
        submitBtn: {
          // long:false,
          innerText:"保存",          
          size:"default",
          // col:{
          //   span:2,
          // }
        },
        resetBtn:{
          type:"default",
          size:"default",
          // long:false,
          show:true
        }
      },
    };
  },
  props: {
    row: {
      type: Object,
    },
    api: {
      type: Object,
    },
  },
  created() {
    this.init();
  },
  components: {},
  methods: {
    init() {
      console.table(this.api.base.columns);
      this.rule = this.api.base.columns
        .filter((u) => {
          return u.isPrimaryKey || u.baseType < 2;
        })
        .map((u) => {
          let item = {
            field: u.dbColumnName,
            title: u.columnDescription,
            // value: "",
            col: {
              span: 12,
            },
          };
          this.getInputType(item, u);
          let rules = [];
          //可空校验
          if (!u.isNullable) {
            let required = {
              required: true,
              message: "请输入" + u.columnDescription,
              trigger: "blur",
            };
            rules.push(required);
          }
          if (u.length > 0) {
            rules.push({
              type: "string",
              max: u.length,
              message: "最多" + u.length + "字符",
            });
          }
          if (rules.length > 0) {
            item.validate = rules;
          }
          return item;
        });
      console.table(this.rule);
    },
    getValueType(item) {
      let type = "string";
      if (["smallint", "int", "bigint"].indexOf(item.dataType) > -1) {
        type = "number";
      }
      return type;
    },
    getInputType(item, u) {
      let constrols = [
        {
          id: 0,
          type: "input",
          props: {
            type: "text",
          },
        },
        {
          id: 1,
          type: "input",
          props: {
            type: "textarea",
          },
        },
        {
          id: 2,
          type: "input-number",
        },
        {
          id: 3,
          type: "date-picker",
          props: {
            type: "date",
          },
        },
        {
          id: 4,
          type: "dictionary",
          props: {
            code: `System.DbEnityDefined.Unit`,
          },
        },
        {
          id: 5,
          type: "editor",
          col: {
            span: 24,
          },
        },
        {
          id: 6,
          type: "input-file",
        },
        {
          id: 7,
          type: "input-file",
          props: {},
        },
        {
          id: 8,
          type: "select",
        },
        {
          id: 9,
          type: "input-image",
        },
        {
          id: 10,
          type: "dictionary",
          props: {
            code: `System.DbEnityDefined.Unit`,
          },
        },
        {
          id: 11,
          type: "color-picker",
        },
        {
          id: 12,
          type: "input-icon",
        },
      ];
      if (u.inputType < constrols.length) {
        item = Object.assign(item, constrols[u.inputType]);
      } else {
        item = Object.assign(item, constrols[0]);
      }
      delete item.id;

      // let v=parseInt(Math.random()*(6-0+1)+0,10);
      // item=Object.assign(item,constrols[v])
    },
  },
};
</script>

<style style="less">
</style>
